<%@page import="java.net.InetAddress"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="net.sf.ehcache.Cache,net.sf.ehcache.CacheManager,
			java.util.List,java.text.SimpleDateFormat,
			java.util.Date" %>
<html>
<head>
<title>EhCache管理</title> 

<table width="98%" border="0" cellspacing="1" cellpadding="5"  align="center" bgcolor="#669900">
<tr bgcolor="#f2f8ff"><Td>
<font color=red size=4><b>EhCache管理</b></font>
</td></tr>
</table>
<%
String cacheName = request.getParameter("cacheName");
String echoNum = request.getParameter("echoNum");
if(echoNum==null){
	echoNum="0";
}

SimpleDateFormat sdf =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss SSS");
if(cacheName == null || (cacheName!=null && "".equals(cacheName))){
// 显示全部cache
try{
	int nousenumber=0;  
	CacheManager manager = CacheManager.getInstance();
	String[] cacheNames=manager.getCacheNames();
	int totalnum = cacheNames.length;
	//int usernum = (totalnum - 3)/3;
	StringBuffer nousercache=new StringBuffer("未使用cache：");;
	for(int i=0;i<cacheNames.length;i++){
		Cache   cache = manager.getCache(cacheNames[i]);
		if(cache.getMemoryStoreSize()==0){
			nousenumber++;
			nousercache.append("["+cacheNames[i]+"]");			
		}
	}
	out.println(nousercache.toString());
%>
<BR>
<table width="98%" border="0" cellspacing="1" cellpadding="5"  align="center" >
<tr bgcolor="#f2f8ff">
<td colspan="2"><%=InetAddress.getLocalHost().getHostName() %>共有Cache [<%=totalnum%>]个；实际使用中[<%=totalnum-nousenumber%>] 个</td>
<td colspan="2"> </td>
</tr>
</table>
<BR>
<%

for(int i=0;i<cacheNames.length;i++){
	Cache   cache = manager.getCache(cacheNames[i]);	
	if(cache.getMemoryStoreSize()>0){
	%>
	<table width="98%" border="0" cellspacing="1" cellpadding="5"  align="center" bgcolor="#669900">
	<tr bgcolor="#f2f8ff">
	<td colspan="2">Cache名:<A href="./listEhcacheRegionStatus.jsp?cacheName=<%=cacheNames[i]%>&echoNum=300#detail"><%=cacheNames[i]%></a></td>

	<td>Status=<%=cache.getStatus()%></td>

	<td>
	<!--a href="index.jsp?method=remove&cacheName=<%=cacheNames[i]%>">Remove</a>
	&nbsp;&nbsp;<a href="listEhcacheRegionStatus.jsp?method=detail&cacheName=<%=cacheNames[i]%>">Detail</a--> 
	</td>

	</tr>

	<tr bgcolor="#FFFFFF">
	<td><font color=red>MaxElementsInMemory=<%=cache.getStatistics().getMemoryStoreObjectCount()%></font></td>
	<td><font color=red>命中:<%=cache.getStatistics().getInMemoryHits()%></font></td>
	<td>MemoryStoreHitCount=<%=cache.getStatistics().getInMemoryHits()%></td>
	<td>MissCountNotFound=<%= cache.getStatistics().getInMemoryMisses()%></td>
	</tr>

	 <tr bgcolor="#f2f8ff">
	<td><font color=red>MemoryStoreSize=<%=cache.getStatistics().getMemoryStoreObjectCount()%></font></td>
	<td>AverageGetTime=<%=cache.getAverageGetTime()%></td>
	<td></td>
	<td></td>
	</tr>
	</table>
	<BR>

	<%
		}else{
			
		}
	}
	}catch(Exception e1){
		System.out.println("get cache excepiton : " + e1.getMessage());
		e1.printStackTrace();
	}

	// End of cacheName is null
	} else {

	try{


	String method = request.getParameter("method");
	CacheManager manager = CacheManager.getInstance();		
	if(cacheName != null){
		Cache   cache = manager.getCache(cacheName);
		if(cache != null){
			if(method != null && method.equals("remove")){
				//cache.removeAll();
				//out.print(cacheName + "  has removed !!");
			} else {
				List keys=cache.getKeys();
	%>
	<a name="detail"></a>
	<table width="98%" border="0" cellspacing="1" cellpadding="5"  align="center" >
	<tr >
		<td>CacheName:&nbsp;<%=cacheName%> </td>
		<td>CacheSize:&nbsp;<%=keys.size()%> </td>
		<td colspan=4>Remove<!--a href="listEhcacheRegionStatus.jsp?method=remove&cacheName=<%=cacheName%>">Remove</a--> 
		</td>
	</tr>
	<tr >
		<td class="titletd">Num</td>	
		<td class="titletd">action</td>
		<td class="titletd">cacheKeys</td>
		<td class="titletd">hitcount</td>
		<td class="titletd">CreationTime</td>
		<td class="titletd">LastAccessTime</td>
	</tr>
	<%
	try{
	  
		String key="";
		for(int j=0;j<keys.size();j++){
			String className  = keys.get(j).getClass().getName();			
	%>
		<tr >
			<td ><%=j%></td>		
			<td><a target="blank" href="showCacheElement.jsp?cacheName=<%=cacheName%>&key=<%=keys.get(j)%>&className=<%=className%>">showDetail</a>&nbsp;<!--a href="showCacheEwwwwddlement.jsp?cacheName=<%=cacheName%>&key=<%=keys.get(j)%>&className=<%=className%>&method=remove">RemoveThis</a-->&nbsp;</td>
			<td ><%=keys.get(j)%></td>
			<%
				if (j<=Integer.parseInt(echoNum) || j+5>keys.size()){%>
				<td ><%=cache.get(keys.get(j)).getHitCount()%></td>
				<td ><%=sdf.format(new Date(cache.get(keys.get(j)).getCreationTime()))%></td>
				<td ><%=sdf.format(new Date(cache.get(keys.get(j)).getLastAccessTime()))%></td>
			<%}else{%>
				<td colspan=3>&nbsp</td>
			<%}%>
		</tr>
		<%	}
		}catch(Exception e){
			System.out.println("\n" + cacheName + " , Exception : " + e.getMessage());
			e.printStackTrace();
		}
	}
	%>
	</table>
	<%
        }
    }
	}catch(Exception e1){
		System.out.println("get cache excepiton : " + e1.getMessage());
		e1.printStackTrace();
	}


} // End of cacheName is not null
%>
